{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "import pymongo\n",
    "\n",
    "from bson.objectid import ObjectId"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "# client = MongoClient('mongodb://localhost:27017/')\n",
    "client = pymongo.MongoClient(host='mongodb://192.168.3.90', port=27017)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [],
   "source": [
    "#db = client.test\n",
    "db = client['QuoteHK']"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {},
   "outputs": [],
   "source": [
    "collection = db.connectTurnover"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "367360\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "<ipython-input-14-917e9f6d8084>:1: DeprecationWarning: count is deprecated. Use Collection.count_documents instead.\n",
      "  count = collection.find().count()\n"
     ]
    }
   ],
   "source": [
    "count = collection.find().count()\n",
    "print(count)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<bound method Collection.count_documents of Collection(Database(MongoClient(host=['192.168.3.90:27017'], document_class=dict, tz_aware=False, connect=True), 'QuoteHK'), 'connectTurnover')>"
      ]
     },
     "execution_count": 15,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "collection.count_documents"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'_id': ObjectId('5f6ab14a1e70aaa0b6c81da3'), 'Market': 'SH', 'Direction': 'NB', 'Time': 20200923102201000, 'BuyTurnover': 5266744964, 'SellTurnover': 5596941659, 'BuySell': 10863686623}\n"
     ]
    }
   ],
   "source": [
    "\n",
    "from bson.objectid import ObjectId\n",
    "\n",
    "result = collection.find_one({'_id': ObjectId('5f6ab14a1e70aaa0b6c81da3')})\n",
    "print(result)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "raw",
   "metadata": {},
   "source": [
    "# result = collection.insert(student)\n",
    "student1 = {\n",
    "    'id': '20170101',\n",
    "    'name': 'Jordan',\n",
    "    'age': 20,\n",
    "    'gender': 'male'\n",
    "}\n",
    "\n",
    "student2 = {\n",
    "    'id': '20170202',\n",
    "    'name': 'Mike',\n",
    "    'age': 21,\n",
    "    'gender': 'male'\n",
    "}\n",
    "\n",
    "result = collection.insert([student1, student2])\n",
    "print(result)\n",
    "\n",
    "student = {\n",
    "    'id': '20170101',\n",
    "    'name': 'Jordan',\n",
    "    'age': 20,\n",
    "    'gender': 'male'\n",
    "}\n",
    "\n",
    "print(result.inserted_id)\n",
    "\n",
    "result = collection.insert_one(student)\n",
    "\n",
    "result = collection.insert_many([student1, student2])\n",
    "print(result)\n",
    "print(result.inserted_ids)\n",
    "\n",
    "\n",
    "result = collection.find_one({'name': 'Mike'})\n",
    "print(type(result))\n",
    "print(result)\n",
    "\n",
    "from bson.objectid import ObjectId\n",
    "\n",
    "result = collection.find_one({'_id': ObjectId('593278c115c2602667ec6bae')})\n",
    "print(result)\n",
    "\n"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.3"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
